package com.itheima.question.timer;

import com.itheima.admin.dto.QuestionDto;
import com.itheima.admin.dto.QuestionItemDto;
import com.itheima.common.contants.RedisPicConstants;
import com.itheima.common.safe.PicSafe;
import com.itheima.common.safe.TextSafe;
import com.itheima.question.service.QuestionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.Set;

/**
 * @ClassName AuditTask
 * @Description TODO
 * @Author Mister-Lu
 * @Date 2021/12/22
 **/
@Component
@Slf4j
public class AuditTask {
    @Autowired
    private QuestionService questionService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Value("${fileServerUrl}")
    private String fileServerUrl;

    @Scheduled(cron = "0/5 * * * * ?")
    public void auditQuestions() throws UnsupportedEncodingException {
        //1、查询redis中集合的内容
        Set<String> questionIds = redisTemplate.opsForSet().members(RedisPicConstants.AUDIT_QUESTION);
        //log.info("定时任务开始执行："+questionIds);
        //2、遍历集合获取到每一个问题的id，根据问题id查询文本内容
        assert questionIds != null;
        for(String questionId:questionIds ){
            QuestionDto questionDto = questionService.queryById(questionId);
            QuestionItemDto questionItemDto = questionService.queryItemById(questionId);
            log.info(questionDto.getSubject());
            log.info(questionItemDto.getContent());
            TextSafe.textSafe(questionDto.getSubject());
            TextSafe.textSafe(questionItemDto.getContent());
        }

        Set<String> pics = redisTemplate.opsForSet().members(RedisPicConstants.ALL_PIC);
        log.info("定时任务开始执行："+fileServerUrl+pics);
        assert pics != null;
        for(String pic : pics){
            //fastDFSClientUtil.delFile(pic);
            PicSafe.picSafe(fileServerUrl+pic);
        }
    }
}
